﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UofCASE.AgileProductLinerDSL;
using Microsoft.VisualStudio.Modeling;

namespace APLDTests {
    [TestClass]
    public class DeleteTest {
        [TestMethod]
        public void TestDeleteAFeature() {
            ModelHelper model = ModelHelper.GetModel(true);

            using (Transaction t = model.Store.TransactionManager.BeginTransaction("delete feature 4")) {
                model.GetElement<ModelElement>("feature4").Delete();
                t.Commit();
            }

            //Deleted
            Assert.IsTrue(model.GetElement<FeatureShape>("featureShape4").IsDeleted);
            Assert.IsTrue(model.GetElement<TestPortShape>("testPortShape3").IsDeleted);
            Assert.IsTrue(model.GetElement<TestConnector>("testCon4").IsDeleted);
            Assert.IsTrue(model.GetElement<FeaturePort>("featurePort4").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePortShape4").IsDeleted);

            //Not deleted
            Assert.IsFalse(model.GetElement<ModelElement>("testShape3").IsDeleted);
        }

        [TestMethod]
        public void TestDeleteATestLinkToATestWithOneLink() {
            ModelHelper model = ModelHelper.GetModel(true);

            //Delete the link between feature 4 and test 3
            using (Transaction t = model.Store.TransactionManager.BeginTransaction("test link 4")) {
                model.GetElement<ModelElement>("testConModel4").Delete();
                t.Commit();
            }

            //Deleted
            Assert.IsTrue(model.GetElement<ModelElement>("testPortShape3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testCon4").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testConModel4").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePort4").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePortShape4").IsDeleted);
            
            //Not deleted
            Assert.IsFalse(model.GetElement<ModelElement>("featureShape4").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("testShape3").IsDeleted);
        }

        [TestMethod]
        public void TestDeleteATest() {
            ModelHelper model = ModelHelper.GetModel(true);

            //Delete test 2
            using (Transaction t = model.Store.TransactionManager.BeginTransaction("delete test 2")) {
                model.GetElement<ModelElement>("test2").Delete();
                t.Commit();
            }
            
            //Deleted
            Assert.IsTrue(model.GetElement<ModelElement>("testPortShape2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testPort2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testCon2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testCon3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePort2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePort3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePortShape2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePortShape3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testShape2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("test2").IsDeleted);

            //Not deleted
            Assert.IsFalse(model.GetElement<ModelElement>("featureShape3").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("featureShape2").IsDeleted);
        }

        [TestMethod]
        public void TestDeleteATestLinkToATestWithMoreThanOneLink() {
            ModelHelper model = ModelHelper.GetModel(true);

            //Delete the link between feature 3 and test 2
            using (Transaction t = model.Store.TransactionManager.BeginTransaction("test link 3")) {
                model.GetElement<ModelElement>("testConModel3").Delete();
                t.Commit();
            }

            //Deleted
            Assert.IsTrue(model.GetElement<ModelElement>("testPortShape2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testPort2").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testCon3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("testConModel3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePort3").IsDeleted);
            Assert.IsTrue(model.GetElement<ModelElement>("featurePortShape3").IsDeleted);

            //Not deleted
            Assert.IsFalse(model.GetElement<ModelElement>("featureShape3").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("feature3").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("testShape2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("testCon2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("testConModel2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("featureShape2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("feature2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("featurePort2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("featurePortShape2").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("testPortShape").IsDeleted);
            Assert.IsFalse(model.GetElement<ModelElement>("testPort").IsDeleted);
        }
    }
}
